package question_answer

import (
	"errors"
	"news_for_deepseek_interface/config"
	questionAnswerDto "news_for_deepseek_interface/dto/admin/question_answer"
	"news_for_deepseek_interface/model"
	tencentlke "news_for_deepseek_interface/project/tencent/lke"
	"time"

	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
	tencenterrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
	lke "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lke/v20231130"
)

func Create(params questionAnswerDto.CreateRequest) error {
	db := model.GetInstance().GetDB()

	// 实例化一个请求对象,每个接口都会对应一个request对象
	request := lke.NewCreateQARequest()

	request.BotBizId = common.StringPtr(config.DefaultConfig.Tencent.Lke.BotBizId)
	request.Question = common.StringPtr(params.Question)
	request.Answer = common.StringPtr(params.Answer)
	request.AttrRange = common.Uint64Ptr(uint64(params.TypeClassifyId))

	// 返回的resp是一个ModifyQACateResponse的实例，与请求对象对应
	response, err := tencentlke.LkeClient.CreateQA(request)
	if _, ok := err.(*tencenterrors.TencentCloudSDKError); ok {
		return errors.New("腾讯云接口创建问答失败:" + err.Error())
	}
	if err != nil {
		return errors.New("腾讯云接口创建问答失败1:" + err.Error())
	}

	// // 发布
	// if err := tencentlke.Release(); err != nil {
	// 	return errors.New("同步腾讯云失败:" + err.Error())
	// }

	return db.Create(&model.QuestionAnswerModel{
		QrBizId:            *response.Response.QaBizId,
		Question:           params.Question,
		Answer:             params.Answer,
		TypeClassifyId:     params.TypeClassifyId,
		BusinessClassifyId: params.BusinessClassifyId,
		CreatedAt:          time.Now(),
		UpdatedAt:          time.Now(),
	}).Error
}
